package com.nowcoder.community.controller;

import com.nowcoder.community.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

@Controller
public class DataController {

    @Autowired
    private DataService dataService;

    @RequestMapping(value = "/data", method = {RequestMethod.GET, RequestMethod.POST})
    public String getDataPage() {
        return "/site/admin/data";
    }

    @RequestMapping(value = "/data/uv", method = RequestMethod.POST)
    public String getUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
                        Model model) {
        model.addAttribute("uvResult", dataService.calculateUV(start, end));
        model.addAttribute("uvStartDate", start);
        model.addAttribute("uvEndDate", end);

//        return "site/admin/data";  // 直接返回模板
        return "forward:/data";  // 转发请求，如果用这种方式，则处理 /data 请求的方法必须能接受POST请求，因为当前请求方式是POST
    }

    @RequestMapping(value = "/data/dau", method = RequestMethod.POST)
    public String getDAU(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
                         Model model) {
        model.addAttribute("dauResult", dataService.calculateDAU(start, end));
        model.addAttribute("dauStartDate", start);
        model.addAttribute("dauEndDate", end);

//        return "site/admin/data";  // 直接返回模板
        return "forward:/data";  // 转发请求，如果用这种方式，则处理 /data 请求的方法必须能接受POST请求，因为当前请求方式是POST
    }

}
